const { usermanage, updateuserService } = require('../service/usermanage.service')
const { addArticleTypeService } = require('../service/articletypemanage.service')
const { getArticleColumnService,updateArticleColumnService } = require('../service/articleColumnService.service')
class ManageController {
    // 用户列表
    async getUserManage(ctx, next) {
        // 1.获取数据
        const { username = '', is_admin = '', is_delete = 0, createdAtFrom = '', createdAtTo = '', updatedAtFrom = '', updatedAtTo = '', currentPage = 1, pageSize = 10 } = ctx.request.query
        // 2.操作数据库
        try {
            // 调用service层最好加try catch
            const { data } = await usermanage({ username, is_admin, is_delete, createdAtFrom, createdAtTo, updatedAtFrom, updatedAtTo, currentPage, pageSize, is_delete })
            // 3.返回给客户端
            ctx.body = {
                code: 1000,
                data,
                current: currentPage,
                pageSize: pageSize
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', userRegisterError, ctx)
        }
    }
    async updateUserManage(ctx, next) {
        const { user_id, is_admin = '', is_delete } = ctx.request.body
        try {
            const data = await updateuserService({ user_id, is_admin, is_delete })
            ctx.body = {
                code: 1000,
                data,

            }
        } catch (err) {
            console.log(err)
        }
    }

    /***
     *  文章分类
     */

    // 添加
    async addArticleType(ctx, next) {
        const { type } = ctx.request.post
        try {
            // 调用service层最好加try catch
            const { data } = await addArticleTypeService({ type })
            // 3.返回给客户端
            ctx.body = {
                code: 1000,
                data,
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', userRegisterError, ctx)
        }
    }
    /**
     *   专栏管理
     */
    async getArticleColumn(ctx, next) {
        // const {user_id='',createdAtFrom = '', createdAtTo = '',examine='',examine_user='',delete='' } = ctx.request.query
        const { column, user_id = '', createdAtFrom = '', createdAtTo = '', examine = 0, examine_user = '', is_delete = 0, currentPage = 1, pageSize = 10, examine_date_from = '', examine_date_to = '' } = ctx.request.query
        try {
            const { data, total } = await getArticleColumnService({ column, user_id, createdAtFrom, createdAtTo, examine, examine_user, is_delete, currentPage, pageSize, examine_date_from, examine_date_to })
            // 3.返回给客户端
            ctx.body = {
                code: 1000,
                data,
                total,
                current: currentPage,
                pageSize: pageSize
            }
        } catch (err) {
            console.log(err)
        }
    }
    async updateArticleColumn(ctx, next) {
        const {column, user_id = '', examine = 0, examine_user = '',result=''} = ctx.request.body
        try {
            // 调用service层最好加try catch
            const res = await updateArticleColumnService({ column, user_id , examine , examine_user ,result})
            // 3.返回给客户端
            ctx.body = {
                 res
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', userRegisterError, ctx)
        }
    }
}

// 导出实例化的对象
module.exports = new ManageController()